package com.az.miaoauth.util;

import cn.hutool.jwt.JWTUtil;
import com.az.miaoapi.dto.UserDto;
import com.az.miaoauth.domain.po.TheUser;
import com.az.miaoauth.domain.vo.TokenVo;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.HashMap;

public class LoginUtil {


    public static TokenVo login(TheUser user, RedisTemplate<String, Object> redisTemplate) {
        String loginKeyPre = "login_users:";
        String key = loginKeyPre + user.getUserId();
        redisTemplate
                .opsForValue()
                .set(key, new UserDto(user.getUserId(), user.getRole()));
        String token = JWTUtil.createToken(new HashMap<>() {{
            put("userId", user.getUserId());
        }}, "miao".getBytes());
        TokenVo tokenVo = new TokenVo();
        tokenVo.setToken(token);
        tokenVo.setRole(user.getRole());
        return tokenVo;
    }
}
